<?php
/*
 * 销售机会控制器
 */
header("Content-Type: text/html;charset=UTF-8");
class Opportunity extends CI_Controller {
	function __construct() {
		parent::__construct ();
		$this->load->helper ( 'url' );
		$this->load->library ( 'pagination' );
	}

	public function index($offset = '',$error_info = '') {
		$user_name=$this->session->userdata('username');
		if($user_name){
			$this->load->model ( 'opportunity_model', 'salesmodel' );
			$limit = 10;
			$total = $this->salesmodel->count_nums ();
			$data ['total'] = $total;
			$data ['offset'] = $offset;
			$data ['sales'] = $this->salesmodel->list_opportunity ( $limit, $offset );
			$current_total = count ( $data ['sales'] );
			$data ['current_total'] = $offset + $current_total;

			$config ['base_url'] = base_url () . 'index.php/opportunity/index/';
			$config ['total_rows'] = $total;
			$config ['per_page'] = $limit;
			$config ['num_links'] = 4;
			$config ['first_link'] = '首页';
			$config ['last_link'] = '尾页';
			$this->pagination->initialize ( $config );
			$data ['pag_links'] = $this->pagination->create_links ();

			$data ['page_title'] = '销售机会 — 昊美科技CRM系统';
			$data ['class3'] = 'cur';
			if($error_info){
				$data ['error_info'] = '<script type="text/javascript">$(function(){alert('.$error_info.')})</script>';
			}else{
				$data ['error_info'] = '';
			}
			$data ['all_users'] = $this->get_all_users();
			$this->load->view ( 'opportunity_list', $data );
		}else{
			$this->load->view ( 'login');
		}
	}

	public function find_error_drop($error_info){ /*添加,编辑出现条件不满足时调用*/
		$this->index(0,$error_info);
	}


	/*
	 * 删除记录
	 */
	public function delete($id = '') {
		$row = $this->db->delete ( 'opportunity', array ('id' => $id ) );
		if($row > 0){
			$this->db->where('opportunity_id', $id);
			$this->db->delete('opportunity_stage');
		}
		redirect ( 'opportunity/index/' );
	}

	/*
	 * 客户名查询记录
	 */
	public function check($subject = '') {
		$this->db->select ( '*' )->from ( 'customer' )->join ( 'opportunity', 'opportunity.customer_id = customer.id' )->like ( 'opportunity.subject', urldecode ( $subject ) )->order_by ( 'customer.update_time', "desc" );
		$query = $this->db->get ();
		$data ['sales'] = $query->result ();
		$data ['pag_links'] = '';
		$data ['page_title'] = '销售机会 — 昊美科技CRM系统';
		$data ['class3'] = 'cur';
		$this->load->view ( 'opportunity_list', $data );
	}

	/**
	 * 数据所有者查询
	 * */
	public function check_user_messages($id = ''){
		$data['sales'] = $this->list_user_opportunity($id);
		$data ['pag_links'] = '';
		$data ['page_title'] = '销售机会 — 昊美科技CRM系统';
		$data ['class3'] = 'cur';
		$data ['all_users'] = $this->get_all_users();
		$query = $this->db->query('SELECT user_name FROM user where id = '.$id.'');
		$row = $query->row();
		$data ['check_user_name'] = $row->user_name;
		$this->load->view ( 'opportunity_list', $data );
	}

	/*
	 * 查看某条记录
	 */
	public function find($id = '') {
		$data = $this->check_find($id);
		$this->load->view ( 'opportunity_view', $data );
	}
	
	public function console_find_opportunity($id = ''){
		$data = $this->check_find($id);
		$this->load->view ( 'console_opportunity_view', $data );
	}
	
	public function check_find($id = ''){
		$query = $this->db->query('SELECT count(*) as nums FROM opportunity_stage where opportunity_id = '.$id.'');
		$row = $query->row_array();
		if($row['nums'] == 0){ /*添加机会阶段推进历史*/
			$query = $this->db->query('SELECT * FROM opportunity where id = '.$id.'');
			$row = $query->row();

			$opportunity_stage['user_id'] = $this->session->userdata('id');
			$opportunity_stage['remark'] = $row->stage_remark;
			$opportunity_stage['stage'] = $row->stage;
			$opportunity_stage['possibility'] = $row->possibility;
			$opportunity_stage['opportunity_id'] = $id;

			date_default_timezone_set ( 'PRC' );
			$now_time = date ( 'Y-m-d H:i:s', strtotime ( 'now' ) );
			$opportunity_stage['update_time'] = $now_time;

			$row = $this->db->insert('opportunity_stage', $opportunity_stage);
		}

		$query = $this->db->select ( '*' )->from ( 'customer' )->join ( 'opportunity', 'opportunity.customer_id = customer.id' )->join ( 'contacter', 'contacter.id = opportunity.contacter_id' )->where ( 'opportunity.id', $id )->get ();
		$data ['find_sales'] = $query->row ();
		/*获取阶段推进历史记录*/
		$q = $this->db->select ( 'user.user_name as user_name,opportunity_stage.*' )->from ( 'opportunity_stage' )->join ( 'user', 'opportunity_stage.user_id = user.id' )->where ( 'opportunity_stage.opportunity_id', $id )->order_by('opportunity_stage.id','asc')->get ();
		$data ['opportunity_stages'] = $q->result_array ();
		$data['time1'] = array();
		foreach ($data ['opportunity_stages'] as $row){
			$data['time1'][]=$row['update_time'];
		}
		return $data;
	}
	
	/*
	 * 查看某条编辑记录
	 */
	public function edit($id = '') {
		$query = $this->db->query('SELECT count(*) as nums FROM opportunity_stage where opportunity_id = '.$id.'');
		$row = $query->row_array();
		if($row['nums'] == 0){ /*添加机会阶段推进历史*/
			$query = $this->db->query('SELECT * FROM opportunity where id = '.$id.'');
			$row = $query->row();

			$opportunity_stage['user_id'] = $this->session->userdata('id');
			$opportunity_stage['remark'] = $row->stage_remark;
			$opportunity_stage['stage'] = $row->stage;
			$opportunity_stage['possibility'] = $row->possibility;
			$opportunity_stage['opportunity_id'] = $id;

			date_default_timezone_set ( 'PRC' );
			$now_time = date ( 'Y-m-d H:i:s', strtotime ( 'now' ) );
			$opportunity_stage['update_time'] = $now_time;

			$row = $this->db->insert('opportunity_stage', $opportunity_stage);
		}
		$query = $this->db->select ( 'customer.customer_name as customer_name,contacter.name as name,opportunity.*' )->from ( 'customer' )->join ( 'opportunity', 'opportunity.customer_id = customer.id' )->join ( 'contacter', 'contacter.id = opportunity.contacter_id' )->where ( 'opportunity.id', $id )->get ();
		$data ['update_sales'] = $query->row ();
		$data ['customer_name']= $data ['update_sales']->customer_name;

		/*机会阶段推进历史*/
		$q = $this->db->select ( 'user.user_name as user_name,opportunity_stage.*' )->from ( 'opportunity_stage' )->join ( 'user', 'opportunity_stage.user_id = user.id' )->where ( 'opportunity_stage.opportunity_id', $id )->order_by('opportunity_stage.id','asc')->get ();
		$data ['opportunity_stages'] = $q->result ();
		$this->load->view ( 'opportunity_edit', $data );
	}

	/*
	 * 编辑某条纪律
	 */
	public function update() {
		$data_array = $_POST;
		if(!trim($data_array ['customer_name']) || !trim($data_array ['subject']) || !trim($data_array ['contacter_id'])){
			$this->find_error_drop("'添加失败,数据不允许为空'");
			return;
		}
		$id = $data_array ['id'];
		$hidden_stage = $data_array['histage'];
		/*
		 * 查询客户编号和联系人编号
		 */
		$customer_id = '';
		$contacter_id = '';
		$this->db->select ( 'id' )->from ( 'customer' )->where ( 'customer_name', $data_array ['customer_name'] );
		$query = $this->db->get ();
		foreach ( $query->result () as $rows ) {
			$customer_id = $rows->id;
		}

		unset ( $data_array ['customer_name'] );
		unset ( $data_array ['name'] );
		unset ( $data_array ['id'] );
		unset( $data_array['histage'] );

		$now_time = date ( 'Y-m-d H:i:s', strtotime ( 'now' ) );

		$data_array ['customer_id'] = $customer_id;
		$this->db->where('id', $id);
		$this->db->update('opportunity', $data_array);

		if($data_array['stage'] != $hidden_stage){  /*是否修改阶段*/
			$opportunity_stage['user_id'] = $this->session->userdata('id');
			$opportunity_stage['remark'] = $data_array['stage_remark'];
			$opportunity_stage['stage'] = $data_array['stage'];
			$opportunity_stage['possibility'] = $data_array['possibility'];
			$opportunity_stage['opportunity_id'] = $id;
			$opportunity_stage['update_time'] = $now_time;
			$row = $this->db->insert('opportunity_stage', $opportunity_stage);
		}
		redirect ( 'opportunity/index/' );
	}

	/*
	 * 增加联系人
	*/
	public function show_contacter_add($customer_name = ''){
		$customer_name = urldecode($customer_name);
		if($customer_name){
			$role=$this->session->userdata('role');
			if($role == '商务助理' || $role == '经理'){
				$data['customer_name'] = $customer_name;
				/*获取客户联系人*/
				$this->db->select ( 'id' )->from ( 'customer' )->where ( 'customer_name', $customer_name );
				$query = $this->db->get ();
				$row = $query->row();
				$customer_id = $row->id;
				/*获取客户联系人*/
				$this->db->select ( 'id,name' )->from ( 'contacter' )->where ( 'customer_id', $customer_id );
				$query = $this->db->get ();
				$contacter_array = $query->result();
				$data['contacters'] = $contacter_array;
			}
		}else{
			$data['contacters'] = array();
		}

		$this->load->view ( 'opportunity_add',$data);
	}

	public function insert() {
		$data_array = $_POST;
		if(!trim($data_array ['customer_name']) || !trim($data_array ['subject']) || !trim($data_array ['contacter_id'])){
			$this->find_error_drop("'添加失败,数据不允许为空'");
			return;
		}
		/*
		 * 获取客户编号和联系人编号
		*/
		$customer_id = '';
		$this->db->select ( 'id' )->from ( 'customer' )->where ( 'customer_name', $data_array ['customer_name'] );
		$query = $this->db->get ();
		foreach ( $query->result () as $rows ) {
			$customer_id = $rows->id;
		}
		unset ( $data_array ['customer_name'] );
		unset ( $data_array ['name'] );

		date_default_timezone_set ( 'PRC' );
		$now_time = date ( 'Y-m-d H:i:s', strtotime ( 'now' ) );
		$data_array['create_time'] = $now_time;

		$data_array ['customer_id'] = $customer_id;

		$row = $this->db->insert('opportunity', $data_array);

		redirect ( 'opportunity/index/' );
	}

	/*检测销售机会阶段*/
	public function check_opportunity_stage(){
		$data_array = $_POST;
		$opp_id = $data_array['opportunity_id'];
		$query = $this->db->query('SELECT stage as stage FROM opportunity_stage where opportunity_id = '.$opp_id.'');
		$row = $query->row_array();
		$stage=array();
		foreach ($query->result() as $row){
			$stage['stage'][] = $row->stage;
		}
		if(in_array("沟通",$stage['stage']) && in_array("招投标/谈判",$stage['stage']) && in_array("立项",$stage['stage']) && in_array("签合同",$stage['stage'])){
			/*检测对应机会是否已有对应合同*/
			$query = $this->db->query('SELECT count(*) as nums FROM contract where opportunity_id = '.$opp_id.'');
			$row = $query->row_array();
			if($row['nums'] == 0){
				echo '2';
			}else{
				echo '1';
			}
		}else{
			echo '0';
		}
	}
	/*自动搜索客户联系人*/
	public function check_contacter(){
		$data_array = $_POST;

		/*获取对应客户编号*/
		$this->db->select ( 'id' )->from ( 'customer' )->where ( 'customer_name', $data_array ['name'] );
		$query = $this->db->get ();
		$row = $query->row();
		$customer_id = $row->id;
		/*获取客户联系人*/
		$this->db->select ( 'id,name' )->from ( 'contacter' )->where ( 'customer_id', $customer_id );
		$query = $this->db->get ();
		$contacter_array = $query->result_array();
		$json_string = json_encode($contacter_array);
		echo $json_string;
	}

	/*获取所有用户*/
	public function get_all_users(){
		$query = $this->db->query("SELECT * FROM user order by create_time asc");
		return $query->result();
	}

	public function list_user_opportunity($user_id = ''){
		if($user_id == 0){
			redirect ( 'opportunity/index/' );
		}else{
			$this->db->select('customer.customer_name as customer_name,customer.user_id as customer_user_id,opportunity.id as opportunity_id,opportunity.*,contacter.id as contacter_id',false);
			$this->db->from('customer');
			$this->db->join('opportunity', 'opportunity.customer_id = customer.id');
			$this->db->join('contacter', 'contacter.id = opportunity.contacter_id');
			$this->db->where('customer.user_id',$user_id);

			$this->db->order_by('opportunity.create_time', "desc");
			$query = $this->db->get();
			return $query->result();
		}
	}
}
?>